<?php

require_once "vo/User.php";
require_once "tech/DBManager.php";


class UserService
{
	
    /**
     * Get a list of users
     * @returns An Array of User
     */	
    function getAllUsers()
    {
        $users=DBManager::getInstance()->query("select * from users");
        $allUsers = array();
       for($i = 0; $i < sizeof($users); $i++){
       		$u = new User();
       		$u->id=$users[$i]["id"];
       		$u->handle=$users[$i]["handle"];
       		$u->f_name=$users[$i]["f_name"];
       		$u->l_name=$users[$i]["l_name"];
       		$allUsers[]=$u;
       }
       
       return $allUsers;
    }
    
    /**
	 * Inserts/updates a user into the database
     */
    function saveUser($in) 
    {
    	$u = new User();
    	$u->id=$in['id'];
    	$u->handle=$in['handle'];
    	$u->f_name=$in['f_name'];
    	$u->l_name=$in['l_name'];
    	$sql = $this->getSaveUserSql($u);
		DBManager::getInstance()->insertUpdate($sql);
    }
    
    function getSaveUserSql(User $u) {
		if($u->id==null)
	    	$sql="insert into users (handle, f_name, l_name) value (".DBManager::escape($u->handle).DBManager::escape($u->f_name).DBManager::escape($u->l_name, false).")";
		else
			$sql="update users set handle=".DBManager::escape($u->handle)." f_name=".DBManager::escape($u->f_name)." l_name=".DBManager::escape($u->l_name)." where id=".$u->id;
    	
		return $sql;
    }
}

?>
